﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace AlarmClock
{
    public class CnDate
    {
        private DateTime date;
        private ChineseLunisolarCalendar chineseDate = new ChineseLunisolarCalendar();

        public int Year;
        public int Month,lMonth;
        public int Day;
        public int DayOfWeek;

        /** GetLeapMonth(int year)方法返回一个1到13之间的数字， 
            * 比如：1、该年阴历2月有闰月，则返回3 
            * 如果：2、该年阴历8月有闰月，则返回9 
            * GetMonth(DateTime dateTime)返回是当前月份，忽略是否闰月 
            * 比如：1、该年阴历2月有闰月，2月返回2，闰2月返回3 
            * 如果：2、该年阴历8月有闰月，8月返回8，闰8月返回9 
            */
        int leapMonth;

        public CnDate()
        {

        }

        public CnDate(CnDate cnDate)
        {
            this.date = cnDate.getDate();
        }

        public CnDate(DateTime date)
        {
            this.date = date;

            init();
        }

        public static bool operator==(CnDate cnDateA,CnDate cnDateB)
        {
            if (cnDateA.getDate() == cnDateB.getDate())
                return true;
            else return false;
        }

        public static bool operator !=(CnDate cnDateA, CnDate cnDateB)
        {
            if (cnDateA.getDate() != cnDateB.getDate())
                return true;
            else return false;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        public void setDate(DateTime date)
        {
            this.date = date;
        }

        public DateTime getDate()
        {
            return date;
        }

        public void AddDays(int days)
        {
            date = date.AddDays(days);

            init();
        }

        private void init()
        {
            Year = chineseDate.GetYear(date);
            Month = lMonth = chineseDate.GetMonth(date);
            Day = chineseDate.GetDayOfMonth(date);

            leapMonth = chineseDate.GetLeapMonth(Year);//获取第几个月是闰月,等于0表示本年无闰月

            if (leapMonth > 0 && lMonth >= leapMonth)
            {
                Month = lMonth - 1;
            }

            DayOfWeek = (int)date.DayOfWeek;
        }
    }
}
